﻿using System;
using System.Collections.Generic;
using UnityEngine;

public class AssetBundleManager : Singleton<AssetBundleManager>
{
    private AssetBundleComplete onComplete;
    private SceneAssetBundle sceneAssetBundle;
    public void Initialized()
    { 
        sceneAssetBundle = new SceneAssetBundle("");
        StartCoroutine(ManifestLoader.Instance.Load());//初始化主assetbundle 依赖
    }
    public bool isLoad(string bundleName) { return sceneAssetBundle.isLoad(bundleName); }
    public bool isFinish(string bundleName)
    {
        return sceneAssetBundle.isFinish(bundleName);
    }

    /// <summary>
    /// 加载单个assetbundle 并自动加载此包的依赖资源
    /// </summary>
    /// <param name="bundleName"></param>
    /// <param name="onProgress"></param> 
    /// <param name="complete"></param>
    public void LoadAssetbundle(string bundleName, AssetBundleLoadProgress onProgress, AssetBundleComplete complete)
    {
        this.onComplete = complete;
        sceneAssetBundle.LoadAssetbundle(bundleName, onProgress, OnLoadAssetBundleCallBack);
    }
    void OnLoadAssetBundleCallBack(string sceneName, string assetBundleName)
    {
        StartCoroutine(sceneAssetBundle.Load(assetBundleName, onComplete));
    }
    #region 获取已经加载的资源和资源释放

    public AssetBundle Asset(string bundleName)
    {
        return sceneAssetBundle.Asset(bundleName);
    }
    public UnityEngine.Object LoadAsset(string bundleName, string assetName)
    {
        return sceneAssetBundle?.LoadAsset(bundleName,assetName);
    }
    public T LoadAsset<T>(string bundleName, string assetName) where T : UnityEngine.Object
    {
        return sceneAssetBundle?.LoadAsset<T>(bundleName, assetName) as T;
    }

    public T[] LoadAssetAll<T>(string bundleName) where T : UnityEngine.Object
    {
        return sceneAssetBundle?.LoadAssetAll<T>(bundleName);
    }
    /// <summary>
    /// 获取图集资源的全部子集
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="assetName"></param>
    /// <returns></returns>
    public UnityEngine.Object[] LoadAssetWithSubAssets(string bundleName, string assetName)
    {
        return sceneAssetBundle?.LoadAssetWithSubAssets(bundleName,assetName);
    }
    /// <summary>
    /// 卸载资源
    /// </summary>
    /// <param name="asset"></param>
    public void UnLoadAsset(string bundleName, string assetname)
    {
        sceneAssetBundle?.UnLoadAsset(bundleName,assetname);
    }
    /// <summary>
    /// 卸载一个包的所有资源
    /// </summary>
    /// <param name="bundleName"></param>
    public void UnLoadAllAsset(string bundleName)
    {
        sceneAssetBundle?.UnLoadAllAsset(bundleName);
    }
    /// <summary>
    /// 卸载所有资源
    /// </summary>
    public void UnLoadAll()
    {
        sceneAssetBundle?.UnLoadAll();
    }
    /// <summary>
    /// 卸载全部包和对应的资源
    /// </summary>
    public void DisposeAllAndUnLoadAll()
    {
        sceneAssetBundle?.DisposeAllAndUnLoadAll();
    }
    /// <summary>
    /// 释放assetbundle资源
    /// </summary>
    public void Dispose(string bundleName)
    {
        sceneAssetBundle?.Dispose(bundleName);
    }
    /// <summary>
    /// 卸载全部资源包
    /// </summary>
    public void DisposeAll()
    {
        sceneAssetBundle?.DisposeAll();
    }
    /// <summary>
    /// 测试专用，获取当前assetbundle资源包的已经加载好的全部资源名称
    /// </summary>
    /// <returns></returns>
    public string[] GetAllAssetNames(string bundleName)
    {
        return sceneAssetBundle?.GetAllAssetNames(bundleName);
    }
    public void Trace(string bundleName)
    {
        sceneAssetBundle?.Trace(bundleName);
    }
    #endregion


    // .... todo 加载场景资源 暂时没实现，底层有实现，需要这这里做加载场景操作
}